package com.eliza.cmps.activity

import android.content.Context
import android.os.Bundle
import android.view.View
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import com.eliza.cmps.R
import com.eliza.library.tools.InfoTools


class CheckBoxActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_checkbox_radio)

        checkBoxFun()

        radioFun()
    }

    fun radioFun() {
        val radgroup = findViewById<RadioGroup>(R.id.radioGroup).apply {
            setOnCheckedChangeListener { group, checkedId ->
                val radbtn = findViewById<RadioButton>(checkedId)
                InfoTools.ToastTools(applicationContext, "你选了: ${radbtn.text}")
            }
        }


        findViewById<Button>(R.id.radio_btnpost).setOnClickListener {
            for (i in 0 until radgroup.childCount) {
                val rd = radgroup.getChildAt(i) as RadioButton
                if (rd.isChecked) {
                    InfoTools.ToastTools(this, "你选择的是: ${rd.text}")
                }
            }
        }

    }

    fun checkBoxFun() {
        val handler = Handler(this)
        findViewById<CheckBox>(R.id.checkBox_cb_one).setOnCheckedChangeListener(handler)
        findViewById<CheckBox>(R.id.checkBox_cb_two).setOnCheckedChangeListener(handler)
        findViewById<CheckBox>(R.id.checkBox_cb_three).setOnCheckedChangeListener(handler)
        findViewById<Button>(R.id.checkBox_btn_send).setOnClickListener(handler)
    }

    companion object {
        class Handler(var context: Context) : View.OnClickListener,
            CompoundButton.OnCheckedChangeListener {
            override fun onClick(p0: View) {
                InfoTools.ToastTools(context, "已提交")
            }

            /**
             * 每次状态 被切换时
             * */
            override fun onCheckedChanged(compoundButton: CompoundButton, p1: Boolean) {
                InfoTools.LogTools(this::class.java.name, compoundButton.text.toString())
                if (compoundButton.isChecked) {
                    InfoTools.ToastTools(context, compoundButton.text.toString())
                }
            }

        }

    }


}

